<?php
/**
* 
*/
class TwitterSearch
{
	
	const BEHAVIOR_ALL = 'all';
	const BEHAVIOR_DEFAULT = 'default';
	
	private $_search = "rainbow";
	private $_title = "It\'s a double rainbow";
	private $_subject = "Across the sky";
	private $_nbTweets = 4;
	private $_interval = 6000; //ms
	private $_width = 250;
	private $_height = 300;
	private $_background = "#8ec1da";
	private $_color = "#ffffff";
	private $_tweetBackground = "#ffffff";
	private $_tweetColor = "#444444";
	private $_tweetLinks = "#1985b5";
	private $_scrollBar = FALSE;
	private $_loop = TRUE;
	private $_live = TRUE;
	private $_hashTags = TRUE;
	private $_timeStamp = TRUE;
	private $_avatars = TRUE;
	private $_topTweets = TRUE;
	private $_behavior = "default"; //default
	private $_isDirty = TRUE;
	private $_button = '';
	
	function __construct($search = "rainbow")
	{
		$this->_search = $search;
	}
	
	public function __set($name, $value)
	{
		if ($name == 'search' ||
			$name == 'title' ||
			$name == 'subject' ||
			$name == 'nbTweets' ||
			$name == 'interval' ||
			$name == 'width' ||
			$name == 'height' ||
			$name == 'background' ||
			$name == 'color' ||
			$name == 'tweetBackground' ||
			$name == 'tweetColor' ||
			$name == 'tweetLinks' ||
			$name == 'scrollBar' ||
			$name == 'loop' ||
			$name == 'live' ||
			$name == 'hashTags' ||
			$name == 'timeStamp' ||
			$name == 'avatars' ||
			$name == 'topTweets' ||
			$name == 'behavior')
			{
				$var = '_' . $name;
				$this->$var = $value;
				$this->_isDirty = TRUE;
			}
	}
	
	private function boolToStr($bool)
	{
		return $bool ? "true" : "false";
	}
	
	public function getWidget()
	{
		if ($this->_isDirty)
		{
			$result = '<script src="http://widgets.twimg.com/j/2/widget.js"></script>';
			$result .= "<script>new TWTR.Widget({version: 2,";
			$result .= "type: 'search',";
			$result .= "search : '" . $this->_search . "',";
			$result .= "interval: " . $this->_interval . ",";
			$result .= "title: '" . $this->_title . "',";
			$result .= "subject: '" . $this->_subject . "',";
			$result .= "width: " . $this->_width . ",";
			$result .= "height: " . $this->_height . ",";
			$result .= "theme: {";
			$result .= "shell: {";
			$result .= "background: '" . $this->_background . "',";
			$result .= "color: '" . $this->_color . "'";
			$result .= "},";
			$result .= "tweets: {";
			$result .= "background: '" . $this->_tweetBackground ."',";
			$result .= "color: '" . $this->_tweetColor . "',";
			$result .= "links: '" . $this->_tweetLinks . "'";
			$result .= "}";
			$result .= "},";
			$result .= "features: {";
			$result .= "scrollbar: " . $this->boolToStr($this->_scrollBar) . ",";
			$result .= "loop: " . $this->boolToStr($this->_loop) . ",";
			$result .= "live: " . $this->boolToStr($this->_live) . ",";
			$result .= "hashtags: " . $this->boolToStr($this->_hashTags) . ",";
			$result .= "timestamp: " . $this->boolToStr($this->_timeStamp) . ",";
			$result .= "avatars: " . $this->boolToStr($this->_avatars) . ",";
			$result .= "toptweets: " . $this->boolToStr($this->_topTweets) . ",";
			$result .= "behavior: '" . $this->_behavior . "'";
			$result .= "}";
			$result .= "}).render().start();";
			$result .= "</script>";
			$this->_button = $result;
			$this->_isDirty = FALSE;
		}
		return $this->_button;
	}
	
	public function __tostring()
	{
		return $this->getWidget();
	}
}
// 
// $search = new TwitterSearch();
// $search->behavior = "default";
// $search->height = 500;
// echo (string)$search;

?>